package com.tiantian.app.activities;

import com.infindo.frame.data.DataProcessor;
import com.infindo.frame.data.net.CallbackListener;
import com.infindo.frame.util.ScreenManager;
import com.tiantian.app.R;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.DialogInterface.OnKeyListener;
import android.graphics.Bitmap;
import android.graphics.Typeface;
import android.os.Bundle;
import android.provider.Settings;
import android.view.KeyEvent;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
 
 

public abstract class SuperActivity extends Activity implements OnClickListener,CallbackListener{
	
	private final String TAG = "SuperActivity";
	
	public static final int NETWORK_INTERRUPT_TIP = 100;
	public static final int NETWORK_TIMEOUT_TIP = 200;
	public static final int MANUAL_SET_ACCESSIP = 300;
	public static final int MANUAL_SET_PHONENUM = 400;
	
	public final static int LOADING_DIALOG = 1000;
	public final static int QUIT_SYSTEM_DIALOG = 2000;
 
 
 
	
	public AlertDialog alertDialog;
	public ProgressDialog progressDialog;
	
	private String errorTitle;
	private String errorMessage;
	
	
	protected TextView superTitle;
	protected Button superRefresh;
	protected Button superMenu;
	protected Button superBack;
	
    protected Button menuHome;
    protected Button menuAnnounce;
    protected Button menuConnection;
    protected Button menuMore;
	
	protected LinearLayout superLayout;
	
	protected Typeface titleFace;
	protected Typeface listFace;
	
 

	public SuperActivity() {
		// TODO Auto-generated constructor stub
		ScreenManager.getScreenManager().pushActivity(this);
 
	}
 
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE); //  
		titleFace=Typeface.createFromAsset(getAssets(), "fonts/Oswald.ttf");
		listFace=Typeface.createFromAsset(getAssets(), "fonts/Oswald.ttf");
 
	}
	
	
 
 
	
	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
	}
	
	@Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
    	// TODO Auto-generated method stub
		
		DataProcessor.clearHandler();
//		Log.e("SuperActivity","onKeyDown:"+keyCode+" event:"+event.getAction());
		
    	if(keyCode == KeyEvent.KEYCODE_BACK){
    		
    		DataProcessor.clearHandler();
    		if(ScreenManager.getScreenManager().getActivitySize()>1){
	    		if(ScreenManager.getScreenManager().currentActivity()!=null){
	    			ScreenManager.getScreenManager().popActivity(ScreenManager.getScreenManager().currentActivity());
	    		}
    		}else{
    			showDialog(QUIT_SYSTEM_DIALOG);
    		}
    	}
    	return false; 
    }
	


    
    public abstract void onResetUI();
	
	public abstract void findView();
	
	public abstract void setListener();

	public abstract void initData();
	
	
	public abstract void callback(String type,int position,Object obj);
	
	
	
	final OnKeyListener dialogListener = new OnKeyListener(){

		@Override
		public boolean onKey(DialogInterface dialog, int keyCode,
				KeyEvent event) {
			// TODO Auto-generated method stub
			
			DataProcessor.clearHandler();
			
	        if(keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount()==0){
	        	DataProcessor.clearHandler();
	        }
			return false;
		}
		
	};
	
	public void onClick(android.view.View view) {
		Intent it;
		switch (view.getId()) {
		case R.id.menuHome:
			it = new Intent(this,PageTeacherHomeActivity.class);
			startActivity(it);
			break;
		case R.id.menuAnnounce:
			it = new Intent(this,PageAnnounceHomeActivity.class);
			startActivity(it);
			break;
		case R.id.menuConnection:
			it = new Intent(this,PageConnectionHomeActivity.class);
			startActivity(it);
			break;
		case R.id.menuMore:
			it = new Intent(this,PageMoreMenuActivity.class);
			startActivity(it);
			break;
		}
	};
	
	protected void closeLoadingDialg(){
		if(progressDialog!=null){
			progressDialog.dismiss();
		}
	}
	
	@Override
	protected Dialog onCreateDialog(final int id) {
		// TODO Auto-generated method stub
        if(isFinishing())
        	return null;
		switch(id){
		case LOADING_DIALOG:
    		if(progressDialog!=null)
    			progressDialog.dismiss();
    		progressDialog = new ProgressDialog(this);
    	    String  message = this.getResources().getString(R.string.loading);
    		progressDialog.setMessage(message);
    		progressDialog.setOnKeyListener(dialogListener);
    		return progressDialog;
		case QUIT_SYSTEM_DIALOG:
    		if(progressDialog!=null)
    			progressDialog.dismiss();
    		 String title  = getString(R.string.app_name);
	   		 alertDialog = new AlertDialog.Builder(this)
		  		.setTitle(title).setMessage("您打算退出这个应用吗?").setPositiveButton("确认", new DialogInterface.OnClickListener() {
					
					@Override
					public void onClick(DialogInterface arg0, int arg1) {
						// TODO Auto-generated method stub
//				        unregisterReceiver(pluginReceiver);
					    ScreenManager.getScreenManager().popAllActivity();
					    System.exit(1);
					}
				}).setNegativeButton("取消", new DialogInterface.OnClickListener() {
					
					@Override
					public void onClick(DialogInterface arg0, int arg1) {
						// TODO Auto-generated method stub
						
					}
				}).create();
	   		 
	   		alertDialog.setOnKeyListener(dialogListener);
			return alertDialog;
		case NETWORK_INTERRUPT_TIP:
    		if(progressDialog!=null)
    			progressDialog.dismiss();
    		alertDialog = new AlertDialog.Builder(this)
	  		.setTitle("").setIcon(android.R.drawable.ic_dialog_info).setMessage("")
            .setPositiveButton( "", new DialogInterface.OnClickListener()
            {
                public void onClick( DialogInterface dialog, int whichButton )
                {
                         
                }
                    
            } ).setNegativeButton("", new DialogInterface.OnClickListener()
            {
                public void onClick( DialogInterface dialog, int whichButton )
                {
 
                }
            } )
            .create();
    		alertDialog.setOnKeyListener(dialogListener);
	  		return  alertDialog;
		case NETWORK_TIMEOUT_TIP:
    		if(progressDialog!=null)
    			progressDialog.dismiss();
    		alertDialog = new AlertDialog.Builder(this)
		    .setTitle("").setIcon(android.R.drawable.ic_dialog_info).setMessage("")
		    .setPositiveButton("", new DialogInterface.OnClickListener()
            {
                public void onClick( DialogInterface dialog, int whichButton )
                {
                	startActivity(new Intent(Settings.ACTION_WIRELESS_SETTINGS));
                }
                    
            } ).setNegativeButton("", new DialogInterface.OnClickListener()
            {
                public void onClick( DialogInterface dialog, int whichButton )
                {
 
                }
            } )
		    .create();
    		alertDialog.setOnKeyListener(dialogListener);
		    return  alertDialog;

		}
		return super.onCreateDialog(id);
	}

	
 
	
 
	
	public void distoryBitmap(Bitmap tempBitmap){
	    if(null!=tempBitmap&&!tempBitmap.isRecycled())	
	    	tempBitmap.recycle();	
	}


}
